using System;
using NUnit.Framework;
using Sample.Domain;
using Sample.Persistence;
using Sample.Service;

namespace SampleTests
{
    [TestFixture]
    public class When_using_a_peer_review_repository
    {
        [SetUp]
        public void ResetDatabase()
        {
            NHibernateHelper.GenerateSchema(false);
        }

        [Test]
        public void Saving_a_peer_review_also_saves_the_associated_reviewee_by_cascade()
        {
            Guid id;
            PeerReview pr;
            using (var session = NHibernateHelper.OpenSession())
            {
                var reviewee = ObjectMother.ValidPerson();
                pr = PeerReview.Create("my review", reviewee, DateTime.UtcNow);
                var ps = new PersonService(session);
                ps.SaveChanges(reviewee);
                var prs = new PeerReviewService(session);
                prs.SaveChanges(pr);
                id = pr.Id;
            }

            using (var session = NHibernateHelper.OpenSession())
            {
                var test = session.Get<PeerReview>(id);
                Assert.AreNotSame(test, pr);
                Assert.AreEqual(test.Id, id);
                Assert.AreEqual(test.ConductedFor.Email, ObjectMother.EmailValid);
            }
        }

        [Test]
        public void Adding_a_reviewer_then_saving_the_reviewer_does_not_save_to_db()
        {
            Person reviewee = ObjectMother.ValidPerson();
            Person reviewer = ObjectMother.ValidPersonTwo();
            PeerReview review = ObjectMother.ValidReview(reviewee);

            using (var session = NHibernateHelper.OpenSession())
            {
                var ps = new PersonService(session);
                ps.SaveChanges(reviewee);
                var prs = new PeerReviewService(session);
                prs.SaveChanges(review);
                ps.SaveChanges(reviewer);
            }

            using (var session = NHibernateHelper.OpenSession())
            {
                var peerReview = session.Get<PeerReview>(review.Id);
                var person = session.Get<Person>(reviewer.Email);
                Assert.AreEqual(0, peerReview.LinkToReviewers.Count);
                Assert.AreEqual(0, person.PeerReviewerFor.Count);
            }
        }

        [Test]
        public void Adding_a_reviewer_works_if_saving_the_peer_review()
        {
            var reviewee = ObjectMother.ValidPerson();
            var reviewer = ObjectMother.ValidPersonTwo();
            var review = ObjectMother.ValidReview(reviewee);

            using(var session = NHibernateHelper.OpenSession())
            {
                var ps = new PersonService(session);
                ps.SaveChanges(reviewee);
                ps.SaveChanges(reviewer);

                var prs = new PeerReviewService(session);
                review.AddReviewer(reviewer, "my review comments");
                prs.SaveChanges(review);
            }
        }
    }
}